package com.lianniu.mgr.baseService.menu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.menu.dao.IOperationManageMapperDao;
import com.lianniu.mgr.baseService.menu.model.OperationManageVo;
import com.lianniu.mgr.baseService.menu.service.IOperationManageService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
/**
 * 

 *
 */
@Service
public class OperationManageServiceImpl implements IOperationManageService {
	  private final static Logger logger = Logger.getLogger(OperationManageServiceImpl.class);
      @Resource
      IOperationManageMapperDao operationManageMapperDao;
      /**
       * 根据菜单id分页查询操作表单
       */
      @Override
      public BaseResult getOperByMenuIdForPage(OperationManageVo vo,PaginationInfo paginationInfo){
    	  logger.info("selectByMenuId begin");
    	  logger.info("menu_id:"+vo.getMenuId());
    	  vo.setPaginationInfo(paginationInfo);
    	  List<OperationManageVo> list = operationManageMapperDao.getOperByMenuIdForPage(vo);
    	  Map<String,Object> map=new HashMap<String,Object>();
    	  map.put("rows", list);
    	  map.put("total", paginationInfo.getTotalRecord());
    	  map.put("curPage", paginationInfo.getCurPage());
    	  return BaseResult.success(map);
      }
      /**
       * 根据id查询操作
       */
      @Override
      public BaseResult getOperById(OperationManageVo vo){
    	  logger.info("selectById begin");
    	  logger.info("id:"+vo.getId());
    	  OperationManageVo OperVo = operationManageMapperDao.getOperById(vo);
    	  Map<String,Object> map=new HashMap<String,Object>();
    	  map.put("OperVo", OperVo);
    	  return BaseResult.success(map);
      }
      /**
       * 新增操作
       */
      @Transactional
      @Override
      public BaseResult addOperation(OperationManageVo vo){
    	  logger.info("addOperation begin");
    	  logger.info("addOperation: input data check begin");
    	  logger.info("OperationManageVo:"+JSON.toJSONString(vo));    	  
    	  dataLogger(vo);
    	  logger.info("addOperation: input data check end");
    	    
    	    return BaseResult.success(operationManageMapperDao.addOperation(vo));
      }
	  /**
	   * 更新操作
	   */
      @Transactional
      @Override
      public BaseResult editOperation(OperationManageVo vo){
    	    logger.info("updateOperation begin");
    	    logger.info("updateOperation: input data check begin");
    	    logger.info("addOperation: input data check begin");
    	    logger.info("OperationManageVo:"+JSON.toJSONString(vo));
	      	dataLogger(vo);
	      	logger.info("updateOperation: input data check end");
	      	return BaseResult.success(operationManageMapperDao.editOperation(vo));
      }
      /**
       * 按照操作id批量删除操作
       */
      @Transactional
      @Override
       public BaseResult delOperation(List<String> list){
    	    logger.info("delOperation begin");
    	    for (int i = 0; i < list.size(); i++) {
				logger.info("第"+i+"个id:"+list.get(i));
			}
    	    return BaseResult.success(operationManageMapperDao.delOperation(list));
       }
      
      //日志打印参数校验为空值的值
      private BaseResult dataLogger(OperationManageVo vo) {
  		BaseResult baseResult;
      	  if (StringUtil.isEmpty(vo.getId())) {
      	      baseResult =
      	          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      	      return baseResult;
      	    }
      	    if (StringUtil.isEmpty(vo.getMenuId())) {
      	      baseResult =
      	          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      	      return baseResult;
      	    }
      	    if (StringUtil.isEmpty(vo.getOpName())) {
      	      baseResult =
      	          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      	      return baseResult;
      	    }
      	    if (StringUtil.isEmpty(vo.getOpDesc())) {
      		      baseResult =
      		          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      		      return baseResult;
      		 }
      	    if (StringUtil.isEmpty(vo.getOpValue())) {
    		      baseResult =
    		          BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    		      return baseResult;
    		 }
      	    return null;
  	}
      
}
